package com.wsl.conyroller;

import com.wsl.pojo.TableNum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:WangShuliang
 * @Description:
 * @Date:Created in 18:27 2018/1/11
 * @Modified by:
 */
@Controller
public class ContrastController {

    @Autowired
    @Qualifier("finJdbcTemplate")
    private JdbcTemplate finJdbcTemplate;


    @RequestMapping("getDatFin")
    @ResponseBody
    public String getOracleData(){
        StringBuffer sb = new StringBuffer();
        //laagent
        sb.append("select 'LAAGENT' as tableName ,count(*) as num from LAAGENT a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append(" 'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");
        sb.append(" union all ");
        //ldplan
        sb.append("select 'LDPLAN', count(*)  from LDPLAN a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append("'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");
        sb.append(" union all ");
        //LKCHARGEDEF
        sb.append("select 'LKCHARGEDEF', count(*)  from LKCHARGEDEF a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append("'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");
        sb.append(" union all ");
        //LKCODEMAPPING
        sb.append("select 'LKCODEMAPPING', count(*) from LKCODEMAPPING a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append("'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");
        sb.append(" union all ");
        //LKTRANSAUTHORIZATION
        sb.append("select 'LKTRANSAUTHORIZATION', count(*) from LKTRANSAUTHORIZATION a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append("'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");
        sb.append(" union all ");
        //LABranchGroup
        sb.append("select 'LABranchGroup', count(*) from LABranchGroup a where to_date(to_char(a.makedate, 'YYYY-MM-DD') || a.maketime,");
        sb.append("'yyyy-mm-dd hh24:mi:ss') <");
        sb.append("to_date(((to_char(trunc(sysdate),'yy-mm-dd'))||',08:00:00'),'yy-mm-dd,hh24:mi:ss')");

        List list = finJdbcTemplate.queryForList(sb.toString());
        List<TableNum> tableNumList = geTableNumListFin(list);
        return tableNumList.toString();
    }


    public List<TableNum> geTableNumListFin(List list){
        List<TableNum> listTableNum = new ArrayList<>();
        TableNum tableNum = null;
        for (int i=0;i<list.size();i++){
            tableNum = new TableNum();
            Map map = (Map) list.get(i);
            tableNum.setTableName(map.get("TABLENAME").toString());
            tableNum.setTableNum(new BigDecimal(map.get("NUM").toString()));
            listTableNum.add(tableNum);
        }
        return listTableNum;
    }

}